<template>
  <div class="retailmember">
    <van-pull-refresh v-model="refreshing" @refresh="OnRefresh">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="OnLoad"
      >
        <div class="list">
          <div class="item" v-for="item in list" :key="item.id">
            <div class="left">
              <div class="img">
                <img :src="item.avatar_cdn" />
              </div>
              <div class="content">
                <div class="nickname">{{ item.nickname }}</div>
                <div class="tel">{{ item.mobile }}</div>
              </div>
            </div>
            <div class="right">{{ item.createtime_date }}</div>
          </div>

          <van-back-top :bottom="'80'" />
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup>
// this代理
const { proxy } = getCurrentInstance();
// 用户信息
const business = proxy.$business;
// 分销会员数据
const list = ref([]);
// 刷新状态
const refreshing = ref(false);
// 懒加载状态
const loading = ref(false);
// 加载状态
const finished = ref(false);
// 当前页
const page = ref(1);

// 挂载前钩子
onBeforeMount(() => OnRefresh());

// 下拉刷新回调函数
const OnRefresh = () => {
  page.value = 1;
  list.value = [];
  finished.value = false;
  loading.value = true;
  refreshing.value = true;

  setTimeout(OnLoad, 1000);
};

// 上拉加载回调函数
const OnLoad = async () => {
  // 如果是下拉刷新，重置其值
  if (refreshing.value === true) {
    refreshing.value = false;
  }
  // 组装参数
  let params = {
    busid: business.value.id,
    page: page.value,
  };
  // 发起请求
  let result = await proxy.$http.post({
    url: "/sale/business",
    params,
  });
  // 取消加载
  loading.value = false;
  // 处理请求结果
  if (result.code === 1) {
    // 获取数据总数
    let count = result.data ? result.data.count : 0;
    // 获取数据并且合并数组
    list.value = result.data ? list.value.concat(result.data.list) : [];
    // 判断是否加载完所有数据
    if (list.value.length >= count) {
      // 完成加载
      finished.value = true;
      return false;
    }
    // 分页+1
    page.value = page.value + 1;
  } else {
    finished.value = true;
  }
};
</script>

<style scoped>
@import url("/assets/css/SaleBusiness.css");
</style>